﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AresCustomYacht
{
    //TODO: Rename to P-Division - load from WorkCategory table
    public partial class P_Division : System.Web.UI.Page
    {
        readonly BL _bl = new BL();
        private int _mode = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            Utility.DisplayPageMessage(this,lblMessage);

            _mode = Utility.GetPageMode(this);
            if (_mode == 1) //Data Mode
            {
                btnAdd.Visible = true;
                btnAddDepartments.Visible = true;
                btnSelect.Visible = true;

            }
            else
            {
                btnAdd.Visible = false;
                btnAddDepartments.Visible = false;
                btnSelect.Visible = true;
            }

            if (!Page.IsPostBack)
            {
                var divisions = _bl.GetDivisions();
                divisions.ForEach(p=> p.Name = p.Code + "  " + p.Name);

                lstDivisions.DataSource = divisions;
                lstDivisions.DataTextField = "Name";
                lstDivisions.DataValueField = "Id";
                lstDivisions.DataBind();
                lstDivisions.SelectedIndex = 0;
            }
        }

        protected void lstDepartments_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            var breadCump = Utility.GetQueryStringItem(this, "Message");

            int boatId = 0;
            if (_mode != 1) //Data Mode
                 boatId = int.Parse(Utility.GetQueryStringItem(this, "BoatId"));
            var linkAddress =
                Utility.Encrypt("Mode=" + _mode + "&BoatId=" + boatId + "&ParentId=" + lstDivisions.SelectedValue + "&Message=" + breadCump + ">" + lstDivisions.SelectedItem.Text);
            Response.Redirect("P-Trade.aspx?k=" + linkAddress);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("P-BatchAdd.aspx?k=" + Utility.Encrypt("Mode=0&What=Division&Message=Add Divisions" + "&ParentId=" + lstDivisions.SelectedValue));
        }

        protected void btnAddDepartments_Click(object sender, EventArgs e)
        {
            Response.Redirect("P-BatchAdd.aspx?k=" + Utility.Encrypt("Mode=0&What=Trade&Message=Add Trades to [" + lstDivisions.SelectedItem.Text + "]&ParentId=" + lstDivisions.SelectedValue ));
        }
    }
}